package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.RestaurantFragement.Fragment1;
import com.baidumap.administrator.happystudy.RestaurantFragement.Fragment2;
import com.baidumap.administrator.happystudy.RestaurantFragement.Fragment3;
import com.baidumap.administrator.happystudy.RestaurantFragement.Fragment4;
import com.baidumap.administrator.happystudy.RestaurantFragement.Fragment5;

import java.util.ArrayList;




public class RestaurantActivity extends FragmentActivity implements OnClickListener {

	// 定义3个Fragment的对象
	private Fragment1 fg1;
	private Fragment2 fg2;
	public Fragment3 fg3;
	private Fragment4 fg4;
	public Fragment5 fg5;

	// 帧布局对象,就是用来存放Fragment的容器
	private FrameLayout flayout;

	// 定义底部导航栏的三个布局
	private RelativeLayout nearby_layout;
	private RelativeLayout community_layout;
	private RelativeLayout message_layout;
	private RelativeLayout my_layout;

	// 定义底部导航栏中的ImageView与TextView
	private ImageView nearby_image;
	private ImageView community_image;
	private ImageView message_image;
	private ImageView my_image;

	private TextView nearby_text;
	private TextView message_text;
	private TextView community_text;
	private TextView my_text;

	// 定义要用的颜色值
	private int whirt = 0xFFFFFFFF;
	private int gray = 0xFFC0C0C0;
	private int orange = 0xFF2AC5BD;

	// 定义FragmentManager对象
	public FragmentManager fManager;

	public int FragmentIndex = 0;


	public static Context instance = null;

	public boolean PalyFlag = false;
	public int unpayed = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initViews();
		fManager = getSupportFragmentManager();
		setChioceItem(0);// 初始化第一个fragment
	}



	@Override
	protected void onResume() {
		// Toast.makeText(getApplicationContext(), "onResume",
		// Toast.LENGTH_SHORT).show();
		super.onResume();
	}

	@Override
	protected void onRestart() {

		super.onRestart();

	}

	// 完成组件的初始化
	public void initViews() {
		nearby_image = (ImageView) findViewById(R.id.nearby_image);
		community_image = (ImageView) findViewById(R.id.community_image);
		message_image = (ImageView) findViewById(R.id.kanpan_image);
		my_image = (ImageView) findViewById(R.id.my_image);

		nearby_text = (TextView) findViewById(R.id.nearby_text);
		community_text = (TextView) findViewById(R.id.community_text);
		message_text = (TextView) findViewById(R.id.kanpan_text);
		my_text = (TextView) findViewById(R.id.my_text);

		nearby_layout = (RelativeLayout) findViewById(R.id.nearby_layout);
		community_layout = (RelativeLayout) findViewById(R.id.community_layout);
		message_layout = (RelativeLayout) findViewById(R.id.message_layout);
		my_layout = (RelativeLayout) findViewById(R.id.my_layout);

		nearby_layout.setOnClickListener(this);
		community_layout.setOnClickListener(this);
		message_layout.setOnClickListener(this);
		my_layout.setOnClickListener(this);
	}

	// 重写onClick事件
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.nearby_layout:
			setChioceItem(0);
			break;
		case R.id.community_layout:
			setChioceItem(1);
			break;
		case R.id.message_layout:
			setChioceItem(2);
			break;
		case R.id.my_layout:
			setChioceItem(3);
			break;
		default:
			break;
		}

	}

	// 定义一个选中一个item后的处理
	public void setChioceItem(int index) {
		// 重置选项+隐藏所有Fragment
		FragmentTransaction transaction = fManager.beginTransaction();
		clearChioce();
		hideFragments(transaction);
		switch (index) {
		case 0:
			nearby_image.setImageResource(R.mipmap.index1_pre);
			nearby_text.setTextColor(orange);

			// nearby_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
			if (fg1 == null) {
				// 如果fg1为空，则创建一个并添加到界面上
				fg1 = new Fragment1();
				transaction.add(R.id.content, fg1);

			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fg1);

			}
			FragmentIndex = 0;

			break;

		case 1:

			community_image.setImageResource(R.mipmap.index2_pre);
			community_text.setTextColor(orange);

			if (fg2 == null) {
				// 如果fg1为空，则创建一个并添加到界面上
				fg2 = new Fragment2();
				transaction.add(R.id.content, fg2);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fg2);

			}
			FragmentIndex = 1;

			break;

		case 2:
			message_image.setImageResource(R.mipmap.index3_pre);
			message_text.setTextColor(orange);

			// message_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
			if (fg3 == null) {
				// 如果fg1为空，则创建一个并添加到界面上
				fg3 = new Fragment3();
				transaction.add(R.id.content, fg3);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fg3);
			}
			FragmentIndex = 2;
			break;
		case 3:
			my_image.setImageResource(R.mipmap.index4_pre);
			my_text.setTextColor(orange);

			// message_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
			if (fg4 == null) {
				// 如果fg1为空，则创建一个并添加到界面上
				fg4 = new Fragment4();
				transaction.add(R.id.content, fg4);
			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fg4);
			}
			FragmentIndex = 3;
			break;
		case 4:
			nearby_image.setImageResource(R.mipmap.index1_pre);
			nearby_text.setTextColor(orange);

			// message_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
			if (fg5 == null) {
				// 如果fg1为空，则创建一个并添加到界面上
				fg5 = new Fragment5();
				transaction.add(R.id.content, fg5);

			} else {
				// 如果MessageFragment不为空，则直接将它显示出来
				transaction.show(fg5);
				// fg5.GetListGetorderPost("1", "1");

			}
			FragmentIndex = 4;
			break;
		}
		transaction.commit();

	}

	// 隐藏所有的Fragment,避免fragment混乱
	public void hideFragments(FragmentTransaction transaction) {
		if (fg1 != null) {
			transaction.hide(fg1);
		}
		if (fg2 != null) {
			transaction.hide(fg2);
		}
		if (fg3 != null) {
			transaction.hide(fg3);
		}
		if (fg4 != null) {
			transaction.hide(fg4);
		}
		if (fg5 != null) {
			transaction.hide(fg5);
		}
	}

	// 定义一个重置所有选项的方法
	public void clearChioce() {
		nearby_image.setImageResource(R.mipmap.index1_nor);
		nearby_layout.setBackgroundColor(whirt);
		nearby_text.setTextColor(gray);
		community_image.setImageResource(R.mipmap.index2_nor);
		community_layout.setBackgroundColor(whirt);
		community_text.setTextColor(gray);
		message_image.setImageResource(R.mipmap.index3_nor);
		message_layout.setBackgroundColor(whirt);
		message_text.setTextColor(gray);
		my_image.setImageResource(R.mipmap.index4_nor);
		my_layout.setBackgroundColor(whirt);
		my_text.setTextColor(gray);
	}

	/**
	 * 以下的几个方法用来，让fragment能够监听touch事件
	 */
	private ArrayList<MyOnTouchListener1> onTouchListeners = new ArrayList<MyOnTouchListener1>(
			10);

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		for (MyOnTouchListener1 listener : onTouchListeners) {
			listener.onTouch(ev);
		}
		return super.dispatchTouchEvent(ev);
	}

	public void registerMyOnTouchListener(MyOnTouchListener1 myOnTouchListener) {
		onTouchListeners.add(myOnTouchListener);
	}

	public void unregisterMyOnTouchListener(MyOnTouchListener1 myOnTouchListener) {
		onTouchListeners.remove(myOnTouchListener);
	}

	public interface MyOnTouchListener1 {
		public boolean onTouch(MotionEvent ev);
	}

}
